package com.bibi.zoo.proxy;

import com.bibi.zoo.demo.IPBean;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.Arrays;
import java.util.List;

public class IPPool {
    public static void main(String[] args) throws IOException {
        List<IPBean> ipBeans = Arrays.asList(new IPBean("61.164.39.66", 53281, IPBean.TYPE_HTTP),
                new IPBean("114.234.83.228", 9000, IPBean.TYPE_HTTP),
                new IPBean("111.77.21.29", 9000, IPBean.TYPE_HTTP),
                new IPBean("60.13.42.33", 9000, IPBean.TYPE_HTTP),
                new IPBean("180.118.128.199", 9000, IPBean.TYPE_HTTP));
        for(int i=0; i<ipBeans.size();i++) {
            RestTemplate restTemplate = new RestTemplateBuilder().build();
            SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
            factory.setReadTimeout(2000);
            factory.setReadTimeout(2000);
            factory.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ipBeans.get(i).getIp(), ipBeans.get(i).getPort())));
            restTemplate.setRequestFactory(factory);
            try {
                HttpStatus statusCode = restTemplate.exchange("http://www.baidu.com", HttpMethod.GET, new HttpEntity<>(null, null), String.class).getStatusCode();
                System.out.println(statusCode  + "======通过");
            } catch (Exception e) {
                System.out.println(e.getMessage());
                System.out.println("不通过");
            }
        }
    }
}
